package com.fangcun.service.api.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.fangcun.bean.api.advertising.request.AdvertisingDisplaySearchRequest;
import com.fangcun.bean.api.advertising.response.AdvertisingDisplaySearchResponse;
import com.fangcun.bean.api.seckill.FlashPromotionGoods;
import com.fangcun.bean.api.seckill.HomeContentResult;
import com.fangcun.bean.api.seckill.HomeFlashPromotion;
import com.fangcun.bean.api.seckill.SmsFlashPromotionExample;
import com.fangcun.bean.api.seckill.SmsFlashPromotionSessionExample;
import com.fangcun.bean.sys.PageBean;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProValiDataException;
import com.fangcun.common.util.date.DateUtils;
import com.fangcun.entity.Goods;
import com.fangcun.entity.SmsFlashPromotion;
import com.fangcun.entity.SmsFlashPromotionSession;
import com.fangcun.mapper.AdvertisingMapper;
import com.fangcun.mapper.GoodsMapper;
import com.fangcun.mapper.SmsFlashPromotionGoodsRelationMapper;
import com.fangcun.mapper.SmsFlashPromotionMapper;
import com.fangcun.mapper.SmsFlashPromotionSessionMapper;
import com.fangcun.service.api.AdvertisingService;
import com.github.pagehelper.PageHelper;

/**
 * @作者： 陳曉鬆 @时间： 2020年6月1日 - 上午9:32:48 @description：->
 */
@Service("advertisingService")
public class AdvertisingDisplayServiceImpl implements AdvertisingService {
	
	@Autowired
	private AdvertisingMapper advertisingMapper;
	@Autowired
    private SmsFlashPromotionMapper flashPromotionMapper;
	@Autowired
	private SmsFlashPromotionGoodsRelationMapper smsFlashPromotionGoodsRelationMapper;
	@Autowired
	private GoodsMapper goodsMapper;
	
	@Autowired
	private SmsFlashPromotionSessionMapper promotionSessionMapper;
	@Override
	public List<AdvertisingDisplaySearchResponse> list(AdvertisingDisplaySearchRequest request) throws ProException {
		List<AdvertisingDisplaySearchResponse> list = advertisingMapper.listForApi(request);
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}

	@Override
	public HomeContentResult content()throws ProException{
		HomeContentResult result = new HomeContentResult();
        //获取秒杀信息
        result.setHomeFlashPromotion(getHomeFlashPromotion());
        //获取新品推荐
        PageBean<Goods> request = new PageBean<Goods>();
		PageHelper.startPage(0, 4);
        result.setNewGoodsList(goodsMapper.getNewGoodsList(request.getParam()));
        return result;
	}
	private HomeFlashPromotion getHomeFlashPromotion() {
        HomeFlashPromotion homeFlashPromotion = new HomeFlashPromotion();
        //获取当前秒杀活动
        Date now = new Date();
        SmsFlashPromotion flashPromotion = getFlashPromotion(now);
        if (flashPromotion != null) {
            //获取当前秒杀场次
            SmsFlashPromotionSession flashPromotionSession = getFlashPromotionSession(now);
            if (flashPromotionSession != null) {
                homeFlashPromotion.setStartTime(flashPromotionSession.getStartTime());
                homeFlashPromotion.setEndTime(flashPromotionSession.getEndTime());
                //获取下一个秒杀场次
                SmsFlashPromotionSession nextSession = getNextFlashPromotionSession(homeFlashPromotion.getStartTime());
                if(nextSession!=null){
                    homeFlashPromotion.setNextStartTime(nextSession.getStartTime());
                    homeFlashPromotion.setNextEndTime(nextSession.getEndTime());
                }
                //获取秒杀商品
                List<FlashPromotionGoods> flashProductList = smsFlashPromotionGoodsRelationMapper.getFlashProductList(flashPromotion.getId(), flashPromotionSession.getId());
                homeFlashPromotion.setProductList(flashProductList);
            }
        }
        return homeFlashPromotion;
    }
	//获取下一个场次信息
    private SmsFlashPromotionSession getNextFlashPromotionSession(Date date) {
        SmsFlashPromotionSessionExample sessionExample = new SmsFlashPromotionSessionExample();
        sessionExample.createCriteria()
                .andStartTimeGreaterThan(date);
        sessionExample.setOrderByClause("start_time asc");
        List<SmsFlashPromotionSession> promotionSessionList = promotionSessionMapper.selectByExample(sessionExample);
        if (!CollectionUtils.isEmpty(promotionSessionList)) {
            return promotionSessionList.get(0);
        }
        return null;
    }
	//根据时间获取秒杀活动
    private SmsFlashPromotion getFlashPromotion(Date date) {
        Date currDate = DateUtils.getDate(date);
        SmsFlashPromotionExample example = new SmsFlashPromotionExample();
        example.createCriteria()
                .andStatusEqualTo(1)
                .andStartDateLessThanOrEqualTo(currDate)
                .andEndDateGreaterThanOrEqualTo(currDate);
        List<SmsFlashPromotion> flashPromotionList = flashPromotionMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(flashPromotionList)) {
            return flashPromotionList.get(0);
        }
        return null;
    }
    //根据时间获取秒杀场次
    private SmsFlashPromotionSession getFlashPromotionSession(Date date) {
        Date currTime = DateUtils.getTime(date);
        SmsFlashPromotionSessionExample sessionExample = new SmsFlashPromotionSessionExample();
        sessionExample.createCriteria()
                .andStartTimeLessThanOrEqualTo(currTime)
                .andEndTimeGreaterThanOrEqualTo(currTime);
        List<SmsFlashPromotionSession> promotionSessionList = promotionSessionMapper.selectByExample(sessionExample);
        if (!CollectionUtils.isEmpty(promotionSessionList)) {
            return promotionSessionList.get(0);
        }
        return null;
    }
}
